package cn.highedu.coolsharkhub.service.impl;

import cn.highedu.coolsharkhub.mapper.BannerMapper;
import cn.highedu.coolsharkhub.pojo.dto.BannerDTO;
import cn.highedu.coolsharkhub.pojo.entity.Banner;
import cn.highedu.coolsharkhub.pojo.vo.BannerHomepageVO;
import cn.highedu.coolsharkhub.pojo.vo.BannerVO;
import cn.highedu.coolsharkhub.service.BannerService;
import cn.highedu.coolsharkhub.service.exception.BlankParameterException;
import cn.highedu.coolsharkhub.service.exception.EntityNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 轮播图业务层实现类
 * 用于操作轮播图数据
 * @author HighEDU
 */
@Service
public class BannerServiceImpl implements BannerService {
    private Logger logger = LoggerFactory.getLogger(BannerServiceImpl.class);

    @Autowired
    private BannerMapper bannerMapper;

    /**
     * 查询所有轮播图
     * @return 所有轮播图
     */
    @Override
    public List<BannerVO> findAll() {
        List<Banner> banners = bannerMapper.selectAll();
        List<BannerVO> bannerVOList = banners.stream().map(banner -> new BannerVO(
                banner.id(),
                banner.image(),
                banner.orderNum()
        )).collect(Collectors.toList());
        return bannerVOList;
    }

    /**
     * 根据轮播图id删除轮播图
     * @param id 轮播图id
     */
    @Override
    public void deleteById(Integer id) {
        if (bannerMapper.deleteById(id) != 1) {
            logger.error("删除轮播图失败，轮播图id为{}", id);
            throw new EntityNotFoundException("删除轮播图失败");
        }
    }

    /**
     * 保存轮播图
     * @param bannerDTO 轮播图信息
     *                     必须的参数
     * @throws BlankParameterException 参数为空
     * @throws EntityNotFoundException 轮播图不存在
     */
    @Override
    public void addBanner(BannerDTO bannerDTO) throws BlankParameterException, EntityNotFoundException {
        if (bannerDTO == null) {
            throw new BlankParameterException("轮播图信息不能为空");
        }
        if (bannerDTO.image() == null || bannerDTO.image().isEmpty()) {
            throw new BlankParameterException("轮播图url不能为空");
        }
        Banner banner = new Banner(null, bannerDTO.image(), bannerDTO.orderNum(), null,null);
        int rows = bannerMapper.insert(banner);
        if (rows != 1) {
            throw new EntityNotFoundException("保存轮播图失败");
        }
    }

    /**
     * 根据轮播图id修改轮播图
     * @param id 轮播图id
     * @param bannerDTO 轮播图
     *                  必须的参数
     * @throws BlankParameterException 参数为空
     * @throws EntityNotFoundException 轮播图不存在
     */
    @Override
    public void updateById(Integer id, BannerDTO bannerDTO) {
        if (bannerDTO == null) {
            throw new BlankParameterException("轮播图信息不能为空");
        }
        if (bannerDTO.image() == null || bannerDTO.image().isEmpty()) {
            throw new BlankParameterException("轮播图url不能为空");
        }
        Banner banner = new Banner(id, bannerDTO.image(), bannerDTO.orderNum(), null,null);
        int rows = bannerMapper.updateById(banner);
        if (rows != 1) {
            throw new EntityNotFoundException("修改轮播图失败");
        }
    }

    /**
     * 查询所有轮播图
     * 用于前端展示，只返回轮播图url和轮播图id
     * @return 所有轮播图
     */
    @Override
    public List<BannerHomepageVO> findAllForHomepage() {
        List<Banner> banners = bannerMapper.selectAll();
        List<BannerHomepageVO> bannerHomepageVOList = banners.stream().map(banner -> new BannerHomepageVO(
                banner.id(),
                banner.image()
        )).collect(Collectors.toList());
        return bannerHomepageVOList;
    }
}
